package homework;

public class HomeWork34 {
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int a=Min(root);
        int b=Max(root);
        System.out.println(a);
        System.out.println(b);
        return b>a+1?false:true;
    }

    private int Min(TreeNode root) {
        if(root==null){
            return 0;
        }
        return Math.min(Min(root.left),Min(root.right))+1;
    }

    private int Max(TreeNode root) {
        if(root==null){
            return 0;
        }
        return Math.max(Max(root.left),Max(root.right))+1;
    }
    public static void main(String[] args) {
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(3);
        TreeNode D = new TreeNode(4);
        TreeNode E = new TreeNode(5);
        TreeNode F = new TreeNode(6);
        TreeNode H = new TreeNode(8);

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.right = F;
        C.left=null;

        HomeWork34 homeWork34=new HomeWork34();
        homeWork34.Max(A);
        System.out.println(homeWork34.isBalanced(A));
    }
}
